# encoding: utf-8
from exts import db
from werkzeug.security import generate_password_hash, check_password_hash
from shortuuid import uuid
import enum
from datetime import datetime
class GenderEnum(enum.Enum):
    MALE = 1
    FEMALE = 2
    SECRET = 3
    UNKNOW = 4

class FrontUser(db.Model):
    __tablename__ = "front_users"
    id = db.Column(db.String(30), primary_key=True, nullable=False, default=uuid)
    username = db.Column(db.String(20), nullable=False, unique=True)
    phone = db.Column(db.String(11), nullable=False, unique=True)
    _password = db.Column(db.String(100), nullable=False)
    email = db.Column(db.String(30), nullable=True)
    real_name = db.Column(db.String(20), nullable=True)
    avatar = db.Column(db.String(100), nullable=True)
    signature = db.Column(db.String(100), nullable=True)
    gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW)
    created_time = db.Column(db.DateTime, default=datetime.now)

    # def __init__(self, *args, **kwargs):
    #     if "password" in kwargs:
    #         self._password = kwargs.get("password")
    #         print(self._password)
    #         kwargs.pop("password")
    #     super(FrontUser, self).__init__(*args, **kwargs)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, password):
        self._password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)